package com.springboot.cloud.demos.feign.rest;

import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.SystemErrorType;
import com.springboot.cloud.demos.entity.form.AddressInfoForm;
import com.springboot.cloud.demos.entity.form.AddressInfoQueryForm;
import com.springboot.cloud.demos.entity.param.AddressInfoQueryParam;
import com.springboot.cloud.demos.entity.po.AddressInfo;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.feign.service.AddressInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
public class AddressInfoController extends BaseController {

    @Autowired
    private AddressInfoService addressInfoService;

    @RequestMapping(value = "/addressInfo", method = RequestMethod.POST)
    public Result save(HttpServletRequest request, @RequestBody AddressInfoForm addressInfo) {
        User user = getUser(request);
        addressInfo.setUserId(user.getId());
        return addressInfoService.add(addressInfo);
    }

    @RequestMapping(value = "/addressInfo", method = RequestMethod.PUT)
    public Result update(HttpServletRequest request, @RequestBody AddressInfoForm addressInfo) {
        User user = getUser(request);
        return addressInfoService.update(addressInfo);
    }

    @RequestMapping(value = "/addressInfo/{id}", method = RequestMethod.GET)
    public Result get(@PathVariable String id) {
        return addressInfoService.addressInfo(id);
    }

    @RequestMapping(value = "/addressInfo/{id}", method = RequestMethod.DELETE)
    public Result delete(@PathVariable String id) {
         addressInfoService.delete(id);
         return Result.success();
    }

    @RequestMapping(value = "/addressInfo", method = RequestMethod.GET)
    public Result getAddressInfo(HttpServletRequest request) {
        User user=getUser(request);
        if(user==null){
            return  Result.fail(SystemErrorType.SYSTEM_BUSY);
        }
        AddressInfoQueryParam param = new AddressInfoQueryParam();
        param.setUserId(user.getId());
        return addressInfoService.conditions(param);
    }
    @RequestMapping(value = "/addressInfo/getDefaultAddressInfo", method = RequestMethod.GET)
    public Result getDefaultAddressInfo(HttpServletRequest request) {
        User user=getUser(request);
        if(user==null){
            return  Result.fail(SystemErrorType.SYSTEM_BUSY);
        }
        AddressInfoQueryParam param = new AddressInfoQueryParam();
        param.setUserId(user.getId());
        param.setDefaultFlag("1");
        return addressInfoService.conditions(param);
    }
}
